package com.xbsg.protocol.xf.test;

import com.xbsg.protocol.xf.RS485Util;
import com.xbsg.protocol.xf.entiy.Protocol;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

/***
 * 解析协议逻辑，
 * 收到16进制字符串，转换成10进制字符串
 */
public class FireRS485Protocol {


    public static String input = "40 40 D8 D4 02 01 00 00 00 00 00 00 14 00 00 00 00 00 00 00 00 00 00 00 02 00 02 09 00 D0 23 23";
/*    public static void main(String[] args) {
        // 16进制字符串，转10进制字符串，
        String result = RS485Util.covertHex2Ten(input);

        System.out.println(result);

        Protocol protocol = new Protocol(result);
        System.out.println(protocol.toString());
        //16进制字符串转字节数组
        String body = result.toString();
        String[] strs1 = body.split(" ");
        byte[] bytes = new byte[32];
        for(int i =0;i<strs1.length;i++){
            //(byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
            bytes[i] = (byte)Integer.parseInt(strs1[i], 16);
        }

        String result1 = gB18030ByteArrayToUtf8String(bytes);
        System.out.println(result1);

    }*/
    //字节数组转 GB18030
    public static String gB18030ByteArrayToUtf8String(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        CharBuffer gb18030 = Charset.forName("GB18030").decode(byteBuffer);
        ByteBuffer utf8 = Charset.forName("UTF8").encode(gb18030);
        return new String(utf8.array());
    }
}
